package com.yidu.DatelessManagement.controller;
import com.yidu.BusinessData.entity.CashAccrued;
import com.yidu.BusinessData.entity.securityAccrued;
import com.yidu.BusinessData.service.CashAccruedService;
import com.yidu.BusinessData.service.SecurityAccruedService;
import com.yidu.BusinessParament.entity.Fund;
import com.yidu.BusinessParament.service.FundService;
import com.yidu.DatelessManagement.entity.ProvisionOfIncome;
import com.yidu.DatelessManagement.service.ProvisionOfIncomeService;
import com.yidu.GlobalManagement.log.util.*;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author 周楠櫻木
 * @version 1.0
 * @date 2023/2/5
 */
@RestController
public class ProvisionOfIncomeController {
    @Resource
    ProvisionOfIncomeService provisionOfIncomeService;
    @Resource
    CashAccruedService cashAccruedService;
    @Resource
    SecurityAccruedService securityAccruedService;
    @Resource
    FundService fundService;
    @Resource
    DbUtil dbUtil;

    /**
     * 现金库存join现金账户查询所有现金利息+分页
     * @param provisionOfIncome ProvisionOfIncome对象
     * @param page 当前页
     * @param limit 每页显示数
     * @return
     */
    @RequestMapping("/selectCashInterest")
    public Map selectCashInterest(ProvisionOfIncome provisionOfIncome, int page, int limit){
        List<ProvisionOfIncome> provisionOfIncomeList = provisionOfIncomeService.selectCashInterest(provisionOfIncome,page,limit);
        int getSumRow = provisionOfIncomeService.getSumRow(provisionOfIncome);
        Map map = new HashMap();
        map.put("code",0);
        map.put("msg","");
        map.put("count",getSumRow);
        map.put("data",provisionOfIncomeList);
        return map;
    }

    //现金的统计
    @RequestMapping("/statisticsCash")
    public int statisticsCash(String cash) {
        int i = 0;
        List<ProvisionOfIncome> provisionOfIncomeList = SysUtil.jsonToArrayList(cash, ProvisionOfIncome.class);
        for (ProvisionOfIncome provisionOfIncome : provisionOfIncomeList) {
            CashAccrued cashAccrued = new CashAccrued();
            cashAccrued.setBusinessTime(provisionOfIncome.getDate());
            cashAccrued.setFundId(provisionOfIncome.getFundId());
            cashAccrued.setCashId(provisionOfIncome.getCashId());
            String cashAccruedId = cashAccruedService.maxSelectCashAccruedId();
            String cId = CreateIDUtil.createId("CA");
            cashAccrued.setCashAccrued(cId);
            cashAccrued.setBusinessType(3);
            cashAccrued.setRemark(provisionOfIncome.getRemark());
            double cashLiXi = NumberUtil.formatNum(provisionOfIncome.getCashLiXi());
            cashAccrued.setMoney(provisionOfIncome.getCashLiXi());
            cashAccrued.setFundFlow(1);
            i = cashAccruedService.addCashAccrued(cashAccrued);
            if(i>0) {
                i = cashAccruedService.deleteCashAccrueds(cashAccrued);
                if (i > 0) {
                    i = cashAccruedService.addCashAccrued(cashAccrued);
                }
            }


        }
        return i;
    }

    /**
     * 债券利息
     * @param provisionOfIncome ProvisionOfIncome对象
     * @param page 当前页
     * @param limit 每页显示数
     * @return
     */
    @RequestMapping("/selectBondInterest")
    public Map selectBondInterest(ProvisionOfIncome provisionOfIncome,int page,int limit){
        List<ProvisionOfIncome> provisionOfIncomeList = provisionOfIncomeService.selectBondInterest(provisionOfIncome,page,limit);
        List<ProvisionOfIncome> getSumRow = provisionOfIncomeService.getSumRowBond(provisionOfIncome);
        Map map = new HashMap();
        map.put("code",0);
        map.put("msg","");
        map.put("count",getSumRow);
        map.put("data",provisionOfIncomeList);
        return map;
    }

    /**
     * 债券的统计
     * @param Securities
     * @param
     * @return
     */
    @RequestMapping("/StatisticalSecurityAccued")
    public int StatisticalSecurityAccued(String Securities) {
        int i=0;
        TxtUtil.sout(Securities);
        List<ProvisionOfIncome> bondInterestList = SysUtil.jsonToArrayList(Securities, ProvisionOfIncome.class);
        for (ProvisionOfIncome provisionOfIncome : bondInterestList) {
            securityAccrued securityAccrued = new securityAccrued();
            securityAccrued.setBusinessTime(provisionOfIncome.getDate());
            System.out.println("asdasddsa"+provisionOfIncome.getFundId());
            securityAccrued.setFundId("159934");
            securityAccrued.setCashId("1");
            String securityAccruedId = cashAccruedService.maxSelectCashAccruedId();
            String cId = CreateIDUtil.createId("SA");
            securityAccrued.setSecurityAccruedId(cId);
            securityAccrued.setSecurityId("600029");
            securityAccrued.setBusinessTime("111111");
            securityAccrued.setMoney(11.1);
            securityAccrued.setBusinessType(3);
            securityAccrued.setRemark("旋木天地");
            System.out.println("cashId=====" + cId);
            double cashLiXi = NumberUtil.formatNum(provisionOfIncome.getCashLiXi());
            securityAccrued.setMoney(provisionOfIncome.getBondLiXi());
            TxtUtil.sout("利息" + cashLiXi);
            securityAccrued.setFundFlow(1);
            i = securityAccruedService.addSecurityAccrued(securityAccrued);
            if(i>0) {
                System.out.println("挤进来");
                i = securityAccruedService.deleteSecurityAccrueds(securityAccrued);
                System.out.println("==============="+i);
                if (i > 0) {
                    i = securityAccruedService.addSecurityAccrued(securityAccrued);
                    System.out.println("执行成功2"+i);
                }
            }

        }
        return i;
    }

    /**
     *  两费统计
     * @return
     */
    @RequestMapping("/getTowFeer")
    public Map getTowFeer(HttpSession session,String foundTime){
        System.out.println("我进了两费统计====="+foundTime);
        String fundId = (String) session.getAttribute("fund");
        ProvisionOfIncome tempFund = new ProvisionOfIncome();
        tempFund.setFundId(fundId);
        //理论上查询所有fund对象
        List<ProvisionOfIncome> fundList = provisionOfIncomeService.selectFund(tempFund,1,9999,foundTime);
        int getSumRow = provisionOfIncomeService.SelectFundCount(tempFund,foundTime);
        List<ProvisionOfIncome> provisionOfIncomeList = new ArrayList<>();
        for (ProvisionOfIncome fund : fundList) {
            ProvisionOfIncome provisionOfIncome = new ProvisionOfIncome();
            provisionOfIncome.setFundId(fund.getFundId());
            provisionOfIncome.setFoundTime(fund.getFoundTime());
            System.out.println("我是两费基金查询的世界"+fund.getFoundTime());
            //管理人费用率
            double custodianCostRate = fund.getCustodianCostRate();
            provisionOfIncome.setCustodianCostRate(custodianCostRate);
            //托管人费用率
            double trusteeCostRate = fund.getTrusteeCostRate();
            provisionOfIncome.setTrusteeCostRate(trusteeCostRate);
            TxtUtil.sout();
            //session.setAttribute("fundNetWorth",2675);
            //获取到净值统计中的资产净值
            provisionOfIncome.setFundScale(fund.getFundScale());
            /*String endTime=null;
            SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
            try {
                Date date = sdf.parse(foundTime);
                Calendar rightNow = Calendar.getInstance();
                rightNow.setTime(date);
                rightNow.add(Calendar.DAY_OF_YEAR,1);
                endTime=sdf.format(rightNow.getTime());
                System.out.println("我是T+1后的时间"+endTime);
            } catch (ParseException e) {
                e.printStackTrace();
            }*/

            double fundNetWorth = -1.0;
            if( session.getAttribute("fundNetWorth")!=null) {
                fundNetWorth= Double.parseDouble(session.getAttribute("fundNetWorth").toString());
            }
            provisionOfIncome.setFundNetWorth(fundNetWorth);
            double guanLiFeiLiXi = fund.getFundScale() * (fund.getTrusteeCostRate() / 100)/365;
            double tuoGuanFeiLiXi = fund.getFundScale() * (fund.getCustodianCostRate() / 100)/365;
            provisionOfIncome.setGuanLiFeiLiXi(guanLiFeiLiXi);
            provisionOfIncome.setTuoGuanFeiLiXi(tuoGuanFeiLiXi);
            provisionOfIncomeList.add(provisionOfIncome);
        }

        Map<String,Object> map = new HashMap<>();
        map.put("code", 0);
        map.put("msg", "");
        map.put("count", getSumRow);
        map.put("data", provisionOfIncomeList);
        return map;
    }

    /**
     * 两费计提
     * @param TowFeer
     * @param session
     * @return
     */
    @RequestMapping("/statisticsTowFeer")
    public int statisticsTowFeer(String TowFeer,HttpSession session){
        int i = 0;
        final int GUAN_LI_FEI = 1;
        final int TUO_GUAN_FEI = 2;
        TxtUtil.sout(TowFeer);
        List<ProvisionOfIncome> provisionOfIncomeList = SysUtil.jsonToArrayList(TowFeer, ProvisionOfIncome.class);
        for (ProvisionOfIncome provisionOfIncome : provisionOfIncomeList) {
            CashAccrued cashAccrued = new CashAccrued();
            String securityAccruedId = cashAccruedService.maxSelectCashAccruedId();
            String sId = CreateIDUtil.createId("CA");
            cashAccrued.setCashAccrued(sId);
            System.out.println("ID"+sId);
            cashAccrued.setBusinessTime(provisionOfIncome.getFoundTime());
            cashAccrued.setFundId(provisionOfIncome.getFundId());
            String defaultCashId = (String) session.getAttribute("defaultCashId");
            defaultCashId="1";
            cashAccrued.setCashId(defaultCashId);
            cashAccrued.setBusinessType(3);
            cashAccrued.setBusinessType(2);
            cashAccrued.setBusinessType(GUAN_LI_FEI);
            cashAccrued.setMoney(provisionOfIncome.getGuanLiFeiLiXi());
            System.out.println("总值===================="+provisionOfIncome.getGuanLiFeiLiXi());
            i = cashAccruedService.deleteCashAccrueds(cashAccrued);
            i = cashAccruedService.addCashAccrued(cashAccrued);

            securityAccruedId = cashAccruedService.maxSelectCashAccruedId();
            sId = CreateIDUtil.createId("CA");
            cashAccrued.setCashAccrued(sId);
            cashAccrued.setBusinessType(TUO_GUAN_FEI);
            //放利息
            cashAccrued.setMoney(provisionOfIncome.getTuoGuanFeiLiXi());
            System.out.println("总值=============="+provisionOfIncome.getTuoGuanFeiLiXi());
            i = cashAccruedService.deleteCashAccrueds(cashAccrued);
            i = cashAccruedService.addCashAccrued(cashAccrued);

        }
        return i;
    }

    /**
     * 获取资产净值的总行数
     *
     * @param data 查询时间
     * @return int 返回的行数
     */
    @RequestMapping("/getChanJinZhiHangShu")
    public int getChanJinZhiHangShu(String data, HttpSession session) {
        int row = 0;
        String fundId = (String) session.getAttribute("fund");
        Fund fund = new Fund();
        fund.setFundId(fundId);
        fund.setFoundTime(data);
        row = provisionOfIncomeService.getJiJinChuangLiShiJian(fund);
        return row;
    }
}
